/**
 * 用于生成slidbar
 * Created by Administrator on 2016/1/27.
 */

(function ($) {
    "use strict";
    moment.locale("zh-cn");
    var mainContent = $("#page-content");

    var initDatepicker = function () {
        $('.date-picker').datepicker({
                startView: 1,
                minViewMode: 1,
                maxViewMode: 1,
                autoclose: true,
                language: 'zh-CN',
                todayHighlight: true
            })
            //show datepicker when clicking on the icon
            .next().on(ace.click_event, function () {
            $(this).prev().focus();
        });
        $('#uploadDate').val(moment().startOf('month').format("YYYY-MM"))
    };
    var pageReady = function () {
        initDatepicker();
        ace.settings.check('breadcrumbs', 'fixed');
        Dropzone.autoDiscover = false;
        try {
            var myDropzone = new Dropzone("#dropzone", {
                url: $.router.upload,
                paramName: "uploadFile", // The name that will be used to transfer the file
                maxFilesize: 5, // MB
                init:function(){
                    this.on("sending",function(file,xhr,data){
                        data.append("hcsj",$("#uploadDate").val())
                    });
                },
                addRemoveLinks: true,
                dictDefaultMessage: '<span class="bigger-150 bolder"><i class="ace-icon fa fa-caret-right red"></i> 拖拽文件到这里</span> 或 \
                    <span class="smaller-80 grey">(点击这里)</span>上传风险文件包 <br /> \
                    <i class="upload-icon ace-icon fa fa-cloud-upload blue fa-3x"></i>'
                ,
                dictResponseError: '上传文件失败!',

                //change the previewTemplate to use Bootstrap progress bars
                previewTemplate: "<div class=\"dz-preview dz-file-preview\">\n  <div class=\"dz-details\">\n    <div class=\"dz-filename\"><span data-dz-name></span></div>\n    <div class=\"dz-size\" data-dz-size></div>\n    <img data-dz-thumbnail />\n  </div>\n  <div class=\"progress progress-small progress-striped active\"><div class=\"progress-bar progress-bar-success\" data-dz-uploadprogress></div></div>\n  <div class=\"dz-success-mark\"><span></span></div>\n  <div class=\"dz-error-mark\"><span></span></div>\n  <div class=\"dz-error-message\"><span data-dz-errormessage></span></div>\n</div>"
            });
            myDropzone.on("complete",function(file){
                var data = JSON.parse(file.xhr.responseText);

                if(data.success==false){
                    bootbox.dialog({
                        message: data.message,
                        buttons: {
                            "success": {
                                "label": "确定",
                                "className": "btn-sm btn-primary"
                            }
                        }
                    });
                }else{
                    bootbox.dialog({
                        message: "上传成功",
                        buttons: {
                            "success": {
                                "label": "确定",
                                "className": "btn-sm btn-primary"
                            }
                        }
                    });
                }
                myDropzone.removeFile(file);
            })
        } catch (e) {
            alert('浏览器版本不支持!');
        }

    };
    mainContent.on("pageshow.0303", function () {
        if (mainContent.find("[data-page=upload]").length > 0) {
            pageReady();
        }
    })
})(jQuery);
